﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using MINERP.CustomerManage.Controller;
using MINERP.COM;

namespace MINERP.CustomerManage
{
    public partial class FrmSearch : Form
    {
        /// <summary>
        /// 查询控制类
        /// </summary>
        private SearchController                    _searchController = null;
        /// <summary>
        /// 订单查询条件类
        /// </summary>
        private criteriaOrderBag                    _orderBagConditionObj = null;
        /// <summary>
        /// 商户查询条件类
        /// </summary>
        private criteriaShop                        _shopConditionObj = null;
        /// <summary>
        /// 产品信息查询条件类
        /// </summary>
        private criteriaProductInfo                 _productInfoObj = null;
        /// <summary>
        /// 员工查询条件
        /// </summary>
        private UserSearchInfo                      _userSearch = null;

        private List<ShopInfo> FilterShopInfos = new List<ShopInfo>();                              //通过助记码筛选得到的商户集合

        /// <summary>
        /// 主界面
        /// </summary>
        public FrmManage _manage
        { get; set; }


        public FrmSearch(SearchController controller)
        {
            this._searchController = controller;
            InitializeComponent();
            Cursor = Cursors.Default;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                switch (tabControlSearch.SelectedTabPageIndex)
                {
                    case 0:
                        //设置查询条件
                        this.GetOrderBagCondition();
                        //根据设置的查询条件进行查询
                        _searchController.Search(_orderBagConditionObj);
                        break;
                    case 1:
                        this.GetShopCondition();
                        _searchController.Search(_shopConditionObj);
                        break;
                    case 2:
                        this.GetProductInfoCondition();
                        _searchController.Search(_productInfoObj);
                        break;
                    case 3:
                        this.GetUserInfoCondition();
                        _searchController.Search(_userSearch);
                        break;
                    case 4:
                        if (string.IsNullOrEmpty(txtPhotoRemarkToSearch.Text.Trim()))
                        {
                            Cursor = Cursors.Default;
                            MessageBox.Show("查询条件不能为空，请重新输入！");
                            return;
                        }
                        _searchController.Search(txtPhotoRemarkToSearch.Text.Trim());
                        break;

                }
                _manage.btnOK_Click();
                Cursor = Cursors.Default;
                this.Close();
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 根据订单输入设置查询条件
        /// </summary>
        private void GetOrderBagCondition()
        {
            _orderBagConditionObj = new criteriaOrderBag();

            if (txtOrdercode.Text != string.Empty)
            {
                _orderBagConditionObj.ordercode = txtOrdercode.Text;
            }

            if (this.dtOrderedStart.DateTime != null)
            {
                _orderBagConditionObj.orderStart = this.dtOrderedStart.DateTime;
            }

            if (this.dtOrderedEnd.DateTime != null)
            {
                _orderBagConditionObj.orderEnd = this.dtOrderedEnd.DateTime;
            }

            if (string.Compare(this.txtctmname.Text, string.Empty) != 0)
            {
                _orderBagConditionObj.ctmName = this.txtctmname.Text;
                _orderBagConditionObj.ctmName = _orderBagConditionObj.ctmName.Replace('[', '(');    //替换中括号
                _orderBagConditionObj.ctmName = _orderBagConditionObj.ctmName.Replace(']', ')');
            }

            if (-1 != this.cmbPriority.SelectedIndex)
            {
                _orderBagConditionObj.priority = this.cmbPriority.SelectedIndex;
            }

            if (this.dtFetchStart.DateTime != null)
            {
                _orderBagConditionObj.fetchStart = this.dtFetchStart.DateTime;
            }

            if (this.dtFetchEnd.DateTime != null)
            {
                _orderBagConditionObj.fetchEnd = this.dtFetchEnd.DateTime;
            }

            if (this.cmbstatus.SelectedIndex != -1)
            {
                _orderBagConditionObj.status = this.cmbstatus.SelectedIndex;
            }
            if (this.txtBagName.Text != string.Empty)
            {
                _orderBagConditionObj.bagName = this.txtBagName.Text;
            }
        }

        /// <summary>
        /// 根据商户信息设置查询条件
        /// </summary>
        private void GetShopCondition()
        {
            _shopConditionObj = new criteriaShop();

            
            _shopConditionObj.shopName = this.cmbShopName.Text;
            if (string.Compare(string.Empty, this.txtRelationName.Text) != 0)
            {
                _shopConditionObj.relationName = this.txtRelationName.Text;
            }
            if (string.Compare(this.txtPhone.Text, string.Empty) != 0)
            {
                _shopConditionObj.phone = this.txtPhone.Text;
            }
            //if (string.Compare(this.txtAddress.Text, string.Empty) != 0)
            //{
            //    _shopConditionObj.adress = this.txtAddress.Text;
            //}
            _shopConditionObj.orderStart = this.dateOrderStart2.DateTime;
            _shopConditionObj.orderEnd = this.dateOrderEnd2.DateTime;
        }

        /// <summary>
        /// 根据产品信息设置查询条件
        /// </summary>
        private void GetProductInfoCondition()
        {
            _productInfoObj = new criteriaProductInfo();

            if (this.cmbProductCate.EditValue != null)
                _productInfoObj.categry = (int)this.cmbProductCate.EditValue;

            if (this.cmbProductName.EditValue != null)
                _productInfoObj.productid = (int)this.cmbProductName.EditValue;

            _productInfoObj.desc = this.txtProductDesc.Text;

            if (txtWidth.Text != string.Empty)
                _productInfoObj.width = int.Parse(txtWidth.Text);

            if (txtHeight.Text != string.Empty)
                _productInfoObj.height = int.Parse(txtHeight.Text);
            if (txtOrderProductCode.Text != string.Empty)
            {
                _productInfoObj.code = this.txtOrderProductCode.Text;
            }
            if (txtPhotoCode.Text != string.Empty)
            {
                _productInfoObj.photoCode = this.txtPhotoCode.Text;
            }
        }

        private void GetUserInfoCondition()
        {
            _userSearch = new UserSearchInfo();

            if (this.cmbChangeColor.EditValue != null)
            {
                _userSearch.ChangeColorUserID = (int)this.cmbChangeColor.EditValue;
            }

            if (this.cmbDesign.EditValue != null)
            {
                _userSearch.DesignUserID = (int)this.cmbDesign.EditValue;
            }

            if (this.cmbModify.EditValue != null)
            {
                _userSearch.ModifyUserID = (int)this.cmbModify.EditValue;
            }

            if (this.cmbOrderUser.EditValue != null)
            {
                _userSearch.OrderUserID = (int)this.cmbOrderUser.EditValue;
            }

            _userSearch.Start = this.dtStart.DateTime;
            _userSearch.End = this.dtEnd.DateTime;
        }


        #region //private helper function

        private int GetStatusByString(string str)
        {
            switch (str)
            {
                case"正常生产" :
                    return 1;
                case"未完成" :
                    return 2;
                case "已完成":
                    return 3;
                case "发生错误":
                    return 4;
            }

            return 0;
        }

        #endregion

        private void FrmSearch_Load(object sender, EventArgs e)
        {
            //初始化产品类别
            this.cmbProductCate.Properties.DataSource = _searchController.Productcates;
            this.cmbShopName.Properties.DataSource = _searchController.ShopInfoList;

            this.cmbOrderUser.Properties.DataSource = _searchController.UserInfos;
            this.cmbOrderUser.Properties.DisplayMember = "UserName";
            this.cmbOrderUser.Properties.ValueMember = "UserID";

            this.cmbModify.Properties.DataSource = _searchController.UserInfos;
            this.cmbModify.Properties.DisplayMember = "UserName";
            this.cmbModify.Properties.ValueMember = "UserID";

            this.cmbDesign.Properties.DataSource = _searchController.UserInfos;
            this.cmbDesign.Properties.DisplayMember = "UserName";
            this.cmbDesign.Properties.ValueMember = "UserID";

            this.cmbChangeColor.Properties.DataSource = _searchController.UserInfos;
            this.cmbChangeColor.Properties.DisplayMember = "UserName";
            this.cmbChangeColor.Properties.ValueMember = "UserID";

            this.dtStart.DateTime = DateTime.Now;
            this.dtEnd.DateTime = DateTime.Now;

            this.dateOrderStart2.DateTime = DateTime.Now.AddDays(-2).Date;
            this.dateOrderEnd2.DateTime = DateTime.Now.Date.AddDays(1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbProductCate_EditValueChanged(object sender, EventArgs e)
        {
            if (this.cmbProductCate.EditValue != null)
                this.cmbProductName.Properties.DataSource = _searchController.GetProductListByCate(int.Parse(this.cmbProductCate.EditValue.ToString()));
        }

        private void FrmSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if(MessageBox.Show("确定退出查询","提示", MessageBoxButtons.OKCancel,MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                    this.Close();
            }
        }

        private void dtOrderedStart_EditValueChanged(object sender, EventArgs e)
        {
            if (this.dtOrderedStart.DateTime == DateTime.MinValue)
            {
                this.dtOrderedStart.EditValue = null;
            }
        }

        private void dtOrderedEnd_EditValueChanged(object sender, EventArgs e)
        {
            if (this.dtOrderedEnd.DateTime == DateTime.MinValue)
            {
                this.dtOrderedEnd.EditValue = null;
            }
        }

        private void dtFetchStart_EditValueChanged(object sender, EventArgs e)
        {
            if (this.dtFetchStart.DateTime == DateTime.MinValue)
            {
                this.dtFetchStart.EditValue = null;
            }
        }

        private void dtFetchEnd_EditValueChanged(object sender, EventArgs e)
        {
            if (this.dtFetchEnd.DateTime == DateTime.MinValue)
            {
                this.dtFetchEnd.EditValue = null;
            }
        }

        private void dtOrderedStart_Properties_VistaTimeProperties_Click(object sender, EventArgs e)
        {
            //this.dtOrderedStart.ClosePopup();
        }

        private void dtOrderedEnd_Properties_VistaTimeProperties_Click(object sender, EventArgs e)
        {
            this.dtOrderedEnd.ClosePopup();
        }

        private void dtFetchStart_Properties_VistaTimeProperties_Click(object sender, EventArgs e)
        {
            this.dtFetchStart.ClosePopup();
        }

        private void dtFetchEnd_Properties_VistaTimeProperties_Click(object sender, EventArgs e)
        {
            this.dtFetchEnd.ClosePopup();
        }

        private void cmbProductName_EditValueChanged(object sender, EventArgs e)
        {

        }

        //清空按钮
        private void btnFindClear_Click(object sender, EventArgs e)
        {
            try
            {
                ClearFind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #region 清空查询条件
        /// <summary>
        /// 清空查询条件
        /// </summary>
        public void ClearFind()
        {
            string tabName = tabControlSearch.SelectedTabPage.Name;
            switch (tabName)
            {
                case "taborderInfo":
                    txtOrdercode.Text = "";
                    dtOrderedStart.Text = "";
                    dtOrderedEnd.Text = "";
                    txtctmname.Text = "";
                    cmbPriority.Text = "";
                    dtFetchStart.Text = "";
                    dtFetchEnd.Text = "";
                    cmbstatus.Text = "";
                    txtBagName.Text = "";
                    break;
                case "tabShopInfo":
                    cmbShopName.EditValue = -1;
                    txtRelationName.Text = "";
                    //txtAddress.Text = "";
                    txtPhone.Text = "";
                    dateOrderStart2.Text = "";
                    dateOrderEnd2.Text = "";
                    break;
                case "tabProductInfo":
                    cmbProductCate.Text = "";
                    cmbProductName.Text = "";
                    txtProductDesc.Text = "";
                    txtWidth.Text = "";
                    txtHeight.Text = "";
                    break;
                case "tabWorkerInfo":
                    cmbOrderUser.EditValue = -1;
                    cmbModify.EditValue = -1;
                    cmbChangeColor.EditValue = -1;
                    cmbDesign.EditValue = -1;
                    dtStart.Text = "";
                    dtEnd.Text = "";
                    break;
            }
        }
        #endregion 

        //商户快速筛选
        private void txtShopZJM_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                string zjm = txtShopZJM.Text.Trim().ToUpper();
                if (zjm.Length == 0)
                {
                    this.cmbShopName.Properties.DataSource = _searchController.ShopInfoList;
                }
                else
                {
                    FilterShopInfos.Clear();
                    foreach (ShopInfo s in _searchController.ShopInfoList)
                    {
                        if (s.ShopZJM != null && s.ShopZJM.Contains(zjm))
                        {
                            FilterShopInfos.Add(s);
                        }
                    }
                    foreach (ShopInfo s in _searchController.ShopInfoList)
                    {
                        if (s.ShopZJM != null && s.ShopName.Contains(zjm))
                        {
                            FilterShopInfos.Add(s);
                        }
                    }
                    this.cmbShopName.Properties.DataSource = FilterShopInfos;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }
}


     
